import React, { Component } from 'react'
import  { connect } from 'react-redux'
import { increment, decrement, incrementAsync} from '../../redux/actions/count'
class Count extends Component {
  state = {
    num: 1
  }
  getSelectVal = (e) => {
    this.setState({
      num: e.target.value * 1
    })
  }
  add = () => {
    this.props.increment(this.state.num)
  }
  sub = () => {
    this.props.decrement(this.state.num)
  }
  addWhenOdd = () => {
    if (this.props.count % 2 !== 0) {
      this.props.increment(this.state.num)
    }
  }
  addAsync = () => {
    this.props.incrementAsync(this.state.num, 1000)
  }
  render() {
    const { count } = this.props
    return (
      <div>
        <h1>我是counter组件</h1>
        <div className="header">合为：{ count }</div>
        <div>
          <select onChange = { this.getSelectVal }>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
          </select>
          <button onClick = { this.add }>+1</button><button onClick = { this.sub }>-1</button><button onClick = { this.addWhenOdd }>奇数加+1</button><button onClick = { this.addAsync }>异步+1</button>
        </div>
      </div>
    )
  }
}

export default connect(
  state => ({ count: state.count }),
  {
    increment,
    decrement,
    incrementAsync
  }
)(Count)